package com.minelx.scriptbuilder.parse.parser;

import com.minelx.scriptbuilder.parse.matcher.PairMatcher;
import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.attribute.value.ArithmeticSequence;
import com.minelx.sequence.IStringScanner;
import com.minelx.sequence.ReturnMatcher;

public class SequenceMatcher implements ReturnMatcher<IVariable, IStringScanner> {
	private final ReturnMatcher<IVariable, IStringScanner> commonValueMatcher;

	private final ReturnMatcher<ArithmeticSequence.Pair, IStringScanner> pairParser;

	public SequenceMatcher(ReturnMatcher<IVariable, IStringScanner> commonValueMatcher) {
		this.commonValueMatcher = commonValueMatcher;
		pairParser = new PairMatcher(commonValueMatcher);
	}

	@Override
	public boolean isMatch(IStringScanner iStringScanner) {
		return true;
	}

	@Override
	public IVariable onMatched(IStringScanner scanner) {
		IVariable firstValue = commonValueMatcher.onMatched(scanner);
		if (!pairParser.isMatch(scanner)) { // there is no pair
			return firstValue;
		}
		ArithmeticSequence result = new ArithmeticSequence(firstValue); // FIXME 2021/3/27  wait for me!!!   ArithmeticSequence to variables
		scanner.sequence(pairParser)
				.forEach(result::operateWith);
		return result.toValue();
	}
}
